<template>
  <el-dialog
    width="90%"
    :title="$t('购买记录')"
    destroy-on-close
    append-to-body
    :visible.sync="show"
    :before-close="cancel"
  >
    <el-row>
      <el-form :model="queryParams" ref="queryForm" size="small" inline v-show="showSearch">
        <el-form-item prop="xx">
          <el-input :placeholder="$t('xx')" v-model="queryParams.xx" clearable @keyup.enter.native="getList" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="getList">
            {{ $t('搜索') }}
          </el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">
            {{ $t('重置') }}
          </el-button>
        </el-form-item>
      </el-form>
    </el-row>

    <el-table
      v-loading="loading"
      show-summary
      :height="calTableHeight() - 100"
      :summary-method="
        (e) =>
          handleSum(e, [
            'turnover',
            'tax',
            'payAmount',
            'freightSkuAmount',
            'skuTotalAmount',
            'useShopCouponAmount',
            'usePlatformCouponAmount',
            'useCouponTotal',
            'usePoints',
            'returnPoints',
          ])
      "
      ref="TableRef"
      :data="tableData"
    >
      <!-- 序号 -->
      <el-table-column :label="$t('序号')" align="center" type="index" />
      <!-- 订单id -->
      <el-table-column show-overflow-tooltip :label="$t('订单号')" width="180" align="center" prop="orderNo" />
      <!-- 收货人 -->
      <el-table-column show-overflow-tooltip :label="$t('收货人')" align="center" prop="receiveName"> </el-table-column>
      <!-- 收货人电话 -->
      <el-table-column show-overflow-tooltip :label="$t('收货人电话')" align="center" prop="receivePhone">
      </el-table-column>

      <!-- 收货人地址 -->
      <el-table-column show-overflow-tooltip :label="$t('收货人地址')" align="center" width="120" prop="address1">
        <template slot-scope="{ row }">
          <span>{{ handleReceiveAddress(row) }}</span>
        </template>
      </el-table-column>
      <!-- 包裹号 -->
      <el-table-column show-overflow-tooltip :label="$t('包裹号')" align="center" prop="packageNo"> </el-table-column>
      <!-- 取消订单 -->
      <el-table-column show-overflow-tooltip :label="$t('取消订单')" align="center" prop="delFlag">
        <template slot-scope="{ row }">
          <!-- 取消订单 0：否，1：是 -->
          <div style="color: green" v-if="row.delFlag == 0">
            {{ $t('否') }}
          </div>
          <div style="color: red" v-if="row.delFlag == 2">
            {{ $t('是') }}
          </div>
        </template>
      </el-table-column>
      <!-- 是否错误 -->
      <el-table-column show-overflow-tooltip :label="$t('是否错误')" align="center" prop="error">
        <template slot-scope="{ row }">
          <!-- 是否有错误 0：无 1：有 -->
          <div style="color: green" v-if="row.error == 0">
            {{ $t('否') }}
          </div>
          <div style="color: red" v-if="row.error == 1">
            <div>{{ $t('是') }}</div>
            <div>{{ row.errorMsg }}</div>
          </div>
        </template>
      </el-table-column>
      <!-- 是否重复 -->
      <el-table-column show-overflow-tooltip :label="$t('是否重复')" align="center" prop="repeatFlag">
        <template slot-scope="{ row }">
          <div v-if="row.repeatFlag == 1">
            <!-- 是 -->
            {{ $t('是') }}
          </div>
          <div v-if="row.repeatFlag == 0">
            <!-- 否 -->
            {{ $t('否') }}
          </div>
        </template>
      </el-table-column>

      <!-- 营业额 -->
      <el-table-column
        show-overflow-tooltip
        :label="$t('营业额') + '(JPY)'"
        align="center"
        prop="turnover"
        width="100"
      />
      <!-- 消费税 -->
      <el-table-column show-overflow-tooltip :label="$t('消费税')" align="center" prop="tax" />
      <!-- 付款金额 -->
      <el-table-column show-overflow-tooltip :label="$t('付款金额')" align="center" prop="payAmount" />
      <!-- 运费合计商品金额 -->
      <el-table-column show-overflow-tooltip :label="$t('运费合计商品金额')" align="center" prop="freightSkuAmount" />
      <!-- 商品合计金额 -->
      <el-table-column show-overflow-tooltip :label="$t('商品合计金额')" align="center" prop="skuTotalAmount" />
      <!-- 使用店铺发行优惠券 -->
      <el-table-column
        show-overflow-tooltip
        :label="$t('使用店铺发行优惠券')"
        align="center"
        prop="useShopCouponAmount"
      />
      <!-- 使用平台发行优惠券 -->
      <el-table-column
        show-overflow-tooltip
        :label="$t('使用平台发行优惠券')"
        align="center"
        prop="usePlatformCouponAmount"
      />
      <!-- 使用优惠券合计 -->
      <el-table-column show-overflow-tooltip :label="$t('使用优惠券合计')" align="center" prop="useCouponTotal" />
      <!-- 使用积分 -->
      <el-table-column show-overflow-tooltip :label="$t('使用积分')" align="center" prop="usePoints" />
      <!-- 返点 -->
      <el-table-column show-overflow-tooltip :label="$t('返点')" align="center" prop="returnPoints" />
      <el-table-column label="sku" align="center" prop="sku" :width="skuWidth">
        <template slot-scope="{ row }">
          <div class="sku-list">
            <div v-for="item in row.skus" class="sku-list-item">
              <div class="skuNo">{{ item.skuNo }}</div>
              <el-image
                :key="Math.random()"
                v-hover="item.imgUrl"
                style="width: 30px; height: 30px"
                fit="contain"
                :src="item.imgUrl + '?x-oss-process=image/resize,w_100/quality,q_70'"
              />
              <div>{{ item.orderSkuNum }}</div>
            </div>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <div slot="footer">
      <el-button @click="cancel">
        {{ $t('关闭') }}
      </el-button>
    </div>
  </el-dialog>
</template>
<script>
// import { api } from '@/api/**'
import handleSum from '@/utils/handleSum'
import { orderList } from '@/api/platform/sellerManage'

export default {
  props: {
    /**
     * formData
     */
    formData: {
      type: Object,
      default: () => ({}),
    },
    /**
     * type
     */
    type: {
      type: String,
    },
    /**
     * 控制显示隐藏
     */
    show: {
      type: Boolean,
      required: true,
      default: false,
    },
  },
  data() {
    return {
      handleSum,
      skuWidth: 0,
      /**显示搜索条件 */
      showSearch: true,
      /**表格选中项 */
      tableCheck: [],
      /**表格数据 */
      tableData: [],
      loading: false,
      /**总条数 */
      total: 0,
      /**分页相关查询参数 */
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
    }
  },
  computed: {},
  watch: {
    show: function (newValue) {
      if (newValue) {
        this.getList()
      }
    },
  },
  methods: {
    handleReceiveAddress(row) {
      return (
        (row.address1 || '') +
        (row.address2 || '') +
        (row.address3 || '') +
        (row.address4 || '') +
        (row.address5 || '') +
        (row.address6 || '')
      )
    },

    getList() {
      this.loading = true
      orderList({
        ...this.queryParams,
        clientId: this.formData.id,
      })
        .then((res) => {
          this.tableData = res.rows
          this.total = res.total
          let maxLength = 0
          res.rows.map((i) => {
            if (i.skus.length > maxLength) {
              maxLength = i.skus.length
            }
          })
          this.skuWidth = maxLength * 170
        })
        .finally(() => (this.loading = false))
    },

    cancel() {
      this.$emit('update:show', false)
    },
    /** 重置按钮操作 */
    resetQuery() {
      Object.keys(this.queryParams).forEach((key) => (this.queryParams[key] = null))
      this.queryParams.pageNum = 1
      this.queryParams.pageSize = 10
      this.getList()
    },
  },
}
</script>
<style scoped lang="scss">
.sku-list {
  display: flex;
  white-space: nowrap;

  &-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    border: 1px solid #eee;
    border-radius: 3px;
    padding: 0 2px;
    margin-right: 5px;
    gap: 8px;

    .skuNo {
      white-space: nowrap;
    }
  }
}
</style>
